package com.imu.purse.modules.trade.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.imu.purse.modules.trade.form.TransForm;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.imu.core.base.BaseMapper;
import com.imu.purse.modules.bitcoin.form.BitcoinForm;
import com.imu.purse.modules.trade.entity.TradeInfoEntity;

/**
 * 交易明细
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-22 10:33:20
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
public interface TradeInfoMapper extends BaseMapper<TradeInfoEntity>
{
	/**
     * 分页查询
     * @return
     */
	@Select({
		"<script>",
		    "SELECT i.id,",
					"i.pay_user_id, ",
					"i.rec_user_id, ",
					"i.rec_addr, ",
					"i.order_no, ",
					"i.order_type, ",
					"i.order_state, ",
					"i.smoney, ",
					"i.money, ",
					"i.emoney, ",
					"i.order_create, ",
					"i.detail, ",
					"i.rec_addr, ",
					"i.rec_phone, ",
					"i.trans_id,",
					"i.fee ,",
					"origin_type,",
					"i.total_money,",
					"i.need_money,",
					"i.imu_to_rmb,",
					"i.gb_to_rmb,",
					"i.ytf_type",
			"FROM imu_trade_info i",
			"WHERE 1 = 1 ",
			"<when test = 'payUserId != null'>",
		    	"AND `pay_user_id` = #{payUserId}",
		    "</when>",
			"<when test = 'recUserId != null'>",
		    	"AND `rec_user_id` = #{recUserId}",
		    "</when>",
			"<when test = 'orderNo != null'>",
		    	"AND `order_no` = #{orderNo}",
		    "</when>",
			"<when test = 'orderType != null and orderType != -1'>",
		    	"AND `order_type` = #{orderType}",
		    "</when>",
			"<when test = 'orderState != null'>",
		    	"AND `order_state` = #{orderState}",
		    "</when>",
		    "<when test = 'startTime != null'>",
		    	"AND order_create &gt;= #{startTime}",
		    "</when>",
		    "<when test = 'endTime != null'>",
				"AND order_create &lt;= #{endTime}",
			"</when>",
		    "ORDER BY order_create DESC",
    	"</script>"
	})
    public List<TradeInfoEntity> findByPage(final TradeInfoEntity entity);	
	
	@Select({
		"select count(1) from imu_trade_info "
		+ " where pay_user_id = #{userId} and trans_id = #{transId}"
	})
	public int findCountByUTId(BitcoinForm form);
	
	@Update({
		"update imu_trade_info "
				+ "set order_state = #{orderState} "
				+ " where pay_user_id = #{userId} and trans_id = #{transId} "
				+ " and order_state = 0 and YEAR(order_create) = YEAR(FROM_UNIXTIME(#{transTime}))"
	})
	public int modByBitcoin(BitcoinForm form);
	
	/**
     * 分页查询
     * @return
     */
	@Select({
		"<script>",
		    "SELECT i.id,",
					"i.pay_user_id, ",
					"i.rec_user_id, ",
					"i.rec_addr, ",
					"i.order_no, ",
					"i.order_type, ",
					"i.order_state, ",
					"i.smoney, ",
					"i.money, ",
					"i.emoney, ",
					"i.order_create, ",
					"i.detail, ",
					"i.rec_addr, ",
					"i.rec_phone, ",
					"i.trans_id,",
					"i.fee,",
					"origin_type,",
					"i.total_money,",
					"i.need_money,",
					"i.imu_to_rmb,",
					"i.gb_to_rmb, ",
					"i.ytf_type, ",
					"u.phone ",
		    "FROM imu_trade_info i ",
			"inner join imu_user u ",
			" on i.pay_user_id = u.id ",
		    "WHERE 1 = 1",
			"<when test = 'payUserId != null'>",
		    	"AND i.pay_user_id = #{payUserId}",
		    "</when>",
			"<when test = 'recUserId != null'>",
		    	"AND i.rec_user_id = #{recUserId}",
		    "</when>",
			"<when test = 'orderNo != null'>",
		    	"AND i.order_no = #{orderNo}",
		    "</when>",
			"<when test = 'orderType != null and orderType != -1'>",
		    	"AND i.order_type = #{orderType}",
		    "</when>",
			"<when test = 'orderState != null'>",
		    	"AND `order_state` = #{orderState}",
		    "</when>",
		    "<when test = 'endTime != null'>",
		    	"AND i.order_create &gt;= #{endTime}",
		    "</when>",
		    "<when test = 'startTime != null'>",
				"AND i.order_create &lt;= #{startTime}",
			"</when>",
			"<when test = 'recPhone != null'>",
	    		"AND i.rec_phone = #{recPhone}",
	    	"</when>",
		    "ORDER BY order_create DESC",
    	"</script>"
	})
    public List<TradeInfoEntity> findByPages(final TradeInfoEntity entity);

	@Update({
			"<script>",
				"update imu_trade_info set TRANS_ID = #{transId} where ",
				"<foreach collection=\"ids\" index=\"index\" item=\"id\" open=\"(\" separator=\"or\" close=\")\">",
					"id = #{id}",
				"</foreach>",
			"</script>"
	})
	public int upTIdById(final TransForm entity);

	@Select(
			"select sum(money) from imu_trade_info where pay_user_id = #{userId} " +
					" and datediff(ORDER_CREATE,#{nowDate}) = 0 " +
					" and order_type = 0 and rec_user_id is null"
	)
	public BigDecimal getMoneyByUIdAndDay(@Param("userId")Long userId,@Param("nowDate") Date nowDate);

	@Select(
			"select count(1) from imu_trade_info where pay_user_id = #{userId} " +
					" and order_no = #{orderNo} "
	)
	public int checkConvsion(Map<String,Object> map);
	
	/**
     * 分页查询
     * @return
     */
	@Select({
		"<script>",
		    "SELECT i.id,",
					"i.pay_user_id, ",
					"i.rec_user_id, ",
					"i.rec_addr, ",
					"i.order_no, ",
					"i.order_type, ",
					"i.order_state, ",
					"i.smoney, ",
					"i.money, ",
					"i.emoney, ",
					"i.order_create, ",
					"i.detail, ",
					"i.rec_addr, ",
					"i.rec_phone, ",
					"i.trans_id,",
					"i.fee,",
					"origin_type,",
					"i.total_money,",
					"i.need_money,",
					"i.imu_to_rmb,",
					"i.gb_to_rmb, ",
					"i.ytf_type  ",
		    " FROM imu_trade_info i ",
			" WHERE i.id = #{id}",
    	"</script>"
	})
    public TradeInfoEntity findById(@Param("id") final Long id);
}
